home *** CD-ROM | disk | FTP | other *** search
- /*------------------------------- poly_calls.c -------------------------------*/
- /* Copyright 1989 Brown University -- Jeffrey Vogel */
- /*----------------------------------------------------------------------------*/
-
-
- /*--------------------------------- Includes ---------------------------------*/
- /*----------------------------------------------------------------------------*/
-
- #include "qd_local.h"
-
-
- /*-------------------------------- SetPoints ---------------------------------*/
- /*----------------------------------------------------------------------------*/
-
- static
- void
- SetPoints(poly, d)
- Polygon *poly;
- XPoint d[];
- {
- register int i;
-
- for (i = 0; i < poly->num_vertices; i++) {
- d[i].x = (short) poly->vertices[i].x;
- d[i].y = (short) poly->vertices[i].y;
- }
- d[poly->num_vertices].x = poly->vertices[0].x;
- d[poly->num_vertices].y = poly->vertices[0].y;
- }
-
- /*-------------------------------- CreatePoly --------------------------------*/
- /*----------------------------------------------------------------------------*/
-
- Polygon
- CreatePoly()
- {
- Polygon a;
-
- /*** error check ***/
- if (!QDrunning) {
- QDerror("ERROR CreatePoly: QuickDraw not initialized.");
- return;
- }
-
- a.num_vertices = 0;
- a.vertices = NULL;
-
- return a;
- }
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
- /*--------------------------------- CopyPoly ---------------------------------*/
- /*----------------------------------------------------------------------------*/
-
- Polygon
- CopyPoly(s)
- Polygon s;
- {
- Polygon d;
- int a;
-
- /*** error check ***/
- if (!QDrunning) {
- QDerror("ERROR CopyPoly: QuickDraw not initialized.");
- return;
- }
-
- a = d.num_vertices = s.num_vertices;
- d.vertices = (Point *) malloc(a * sizeof(Point));
- bcopy((char *) s.vertices, (char *) d.vertices,
- a * sizeof(Point));
- }
-
- /*-------------------------------- PolyAddPt ---------------------------------*/
- /*----------------------------------------------------------------------------*/
-
- void
- PolyAddPt(poly, x, y)
- Polygon *poly;
- int x, y;
- {
- /*** error check ***/
- if (!QDrunning) {
- QDerror("ERROR PolyAddPt: QuickDraw not initialized.");
- return;
- }
-
- /*** set number, malloc, and set point ***/
- poly->num_vertices++;
- if (poly->vertices)
- poly->vertices = (Point *) realloc((char *) poly->vertices,
- poly->num_vertices * sizeof(Point));
- else
- poly->vertices = (Point *) malloc(poly->num_vertices * sizeof(Point));
- poly->vertices[poly->num_vertices-1].x = x;
- poly->vertices[poly->num_vertices-1].y = y;
- }
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
- /*------------------------------- PolyQueryPt --------------------------------*/
- /*----------------------------------------------------------------------------*/
-
- void
- PolyQueryPt(poly, index, x, y)
- Polygon poly;
- int index;
- int *x, *y;
- {
- /*** error check ***/
- if (!QDrunning) {
- QDerror("ERROR PolyQueryPt: QuickDraw not initialized.");
- return;
- }
- if ((index > poly.num_vertices) || (index < 1)) {
- QDerror("ERROR PolyQueryPt: invalid index.");
- return;
- }
-
- *x = poly.vertices[index-1].x;
- *y = poly.vertices[index-1].y;
- }
-
-
- /*-------------------------------- FramePoly ---------------------------------*/
- /*----------------------------------------------------------------------------*/
-
- void
- FramePoly(poly)
- Polygon poly;
- {
- XPoint *xpoints;
-
- /*** error check ***/
- if (!QDrunning) {
- QDerror("ERROR FramePoly: QuickDraw not initialized.");
- return;
- }
-
- /*** trivial reject ***/
- if (poly.num_vertices < 3) {
- QDerror("ERROR FramePoly: polygon needs at least 3 vertices.");
- return;
- }
-
- /*** set up points, draw them, and free them ***/
- xpoints = (XPoint *) malloc((poly.num_vertices+1) * sizeof(XPoint));
- SetPoints(&poly, xpoints);
- XDrawLines(QDdisplay, QDwindow, QDgc, xpoints, poly.num_vertices+1,
- CoordModeOrigin);
- free(xpoints);
-
- /*** flush the request ***/
- XFlush(QDdisplay);
- }
-
-
-
-
-
-
-
-
-
-
-
- /*-------------------------------- PaintPoly ---------------------------------*/
- /*----------------------------------------------------------------------------*/
-
- void
- PaintPoly(poly)
- Polygon poly;
- {
- XPoint *xpoints;
-
- /*** error check ***/
- if (!QDrunning) {
- QDerror("ERROR PaintPoly: QuickDraw not initialized.");
- return;
- }
-
- /*** trivial reject ***/
- if (poly.num_vertices < 3) {
- QDerror("ERROR PaintPoly: polygon needs at least 3 vertices.");
- return;
- }
-
- /*** set up points, draw them, and free them ***/
- xpoints = (XPoint *) malloc((poly.num_vertices+1) * sizeof(XPoint));
- SetPoints(&poly, xpoints);
- XFillPolygon(QDdisplay, QDwindow, QDgc, xpoints, poly.num_vertices+1,
- Complex, CoordModeOrigin);
- free(xpoints);
-
- /*** flush the request ***/
- XFlush(QDdisplay);
- }
-
-
- /*-------------------------------- ErasePoly ---------------------------------*/
- /*----------------------------------------------------------------------------*/
-
- void
- ErasePoly(poly)
- Polygon poly;
- {
- XPoint *xpoints;
-
- /*** error check ***/
- if (!QDrunning) {
- QDerror("ERROR ErasePoly: QuickDraw not initialized.");
- return;
- }
-
- /*** trivial reject ***/
- if (poly.num_vertices < 3) {
- QDerror("ERROR ErasePoly: polygon needs at least 3 vertices.");
- return;
- }
-
- /*** set up points, draw them, and free them ***/
- xpoints = (XPoint *) malloc((poly.num_vertices+1) * sizeof(XPoint));
- SetPoints(&poly, xpoints);
- XSetFunction(QDdisplay, QDgc, GXclear);
- XFillPolygon(QDdisplay, QDwindow, QDgc, xpoints, poly.num_vertices+1,
- Complex, CoordModeOrigin);
- XChangeGC(QDdisplay, QDgc, GCFunction, &QDgcValues);
- free(xpoints);
-
-
-
-
- /*** flush the request ***/
- XFlush(QDdisplay);
- }
-
-
-
-
- /*-------------------------------- OffsetPoly --------------------------------*/
- /*----------------------------------------------------------------------------*/
-
- void
- OffsetPoly(poly, dx, dy)
- Polygon *poly;
- int dx, dy;
- {
- register int i;
-
- /*** error check ***/
- if (!QDrunning) {
- QDerror("ERROR OffsetPoly: QuickDraw not initialized.");
- return;
- }
-
- /*** trivial reject ***/
- if (poly->num_vertices < 3) {
- QDerror("ERROR OffsetPoly: polygon needs at least 3 vertices.");
- return;
- }
-
- for (i = 0; i < poly->num_vertices; i++) {
- poly->vertices[i].x += dx;
- poly->vertices[i].y += dy;
- }
- }
-
-
- /*-------------------------------- InsetPoly ---------------------------------*/
- /*----------------------------------------------------------------------------*/
-
- void
- InsetPoly(poly, dx, dy)
- Polygon *poly;
- int dx, dy;
- {
- register int i;
- int t, b, l, r, cx, cy;
-
- /*** error check ***/
- if (!QDrunning) {
- QDerror("ERROR InsetPoly: QuickDraw not initialized.");
- return;
- }
-
- /*** trivial reject ***/
- if (poly->num_vertices < 3) {
- QDerror("ERROR InsetPoly: polygon needs at least 3 vertices.");
- return;
- }
-
-
-
-
-
-
-
-
- /*** get boundaries ***/
- t = b = poly->vertices[0].y;
- r = l = poly->vertices[0].x;
- for (i = 0; i < poly->num_vertices; i++) {
- if (poly->vertices[i].x > r)
- r = poly->vertices[i].x;
- if (poly->vertices[i].x < l)
- l = poly->vertices[i].x;
- if (poly->vertices[i].y > b)
- b = poly->vertices[i].y;
- if (poly->vertices[i].x < t)
- t = poly->vertices[i].y;
- }
-
- /*** get center ***/
- cx = (r + l) / 2;
- cy = (t + b) / 2;
-
- /*** inset each towards center ***/
- for (i = 0; i < poly->num_vertices; i++) {
- if (poly->vertices[i].x < cx)
- poly->vertices[i].x += dx;
- else
- poly->vertices[i].x -= dx;
- if (poly->vertices[i].y < cy)
- poly->vertices[i].y += dy;
- else
- poly->vertices[i].y -= dy;
- }
- }
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-